package com.example.sjjg.utest.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.sjjg.utest.databinding.FragmentDataBinding
import com.example.sjjg.utest.viewmodel.DataViewModel


class DataFragment : Fragment() {
    lateinit var binding : FragmentDataBinding
    lateinit var adapter : DataAdapter
    val viewModel        : DataViewModel by lazy {initViewModel()}

    fun initViewModel()  =  ViewModelProvider(requireActivity()).get(DataViewModel::class.java)
    fun onIncValueClicked(v : View) = viewModel.incValue()

    fun initDataList(){
        adapter                         = DataAdapter()
        adapter.data                    = viewModel.data()
        binding.dataList.adapter        = adapter
        binding.dataList.layoutManager  = LinearLayoutManager(requireContext())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentDataBinding.inflate(inflater,container,false)
        binding.lifecycleOwner  = this
        binding.status          = viewModel.status()
        binding.value           = viewModel.value()
        binding.incBtn.setOnClickListener(this::onIncValueClicked)
        initDataList()
        return binding.root
    }
}